package com.hankol.newretail.adapter

import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.baselibrary.extension.checkNotNull
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.newretail.R
import com.hankol.newretail.bean.Serv
import com.hankol.newretail.widget.EvaluationRatingBar
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout
import com.zhy.view.flowlayout.TagView

/**
 * @author: 关忆北、
 * @date: 2022/8/8
 * @Des：评价配置列表
 */
class EvaluationConfigAdapter : BaseQuickAdapter<Serv, BaseViewHolder>(R.layout.item_service_evaluation) {

    override fun convert(helper: BaseViewHolder, item: Serv) {
        helper.setText(R.id.tv_service_name, item.commentType)
        var tagLayout: TagFlowLayout = helper.getView(R.id.tf_value)
        tagLayout.adapter = object : TagAdapter<String>(item.commentQuestionList) {
            override fun getView(parent: FlowLayout?, position: Int, s: String?): View? {
                val tv = LayoutInflater.from(mContext).inflate(R.layout.item_evaluation_value, parent, false) as TextView
                tv.text = s
                return tv
            }

        }
        item.commentQuestionListTemp = ArrayList()
        tagLayout.setOnTagClickListener { view, position, parent ->
            if ((tagLayout.getChildAt(position) as TagView).isChecked) {
                if (item.commentQuestionList.checkNotNull() && position < item.commentQuestionList.size) {
                    item.commentQuestionListTemp.add(item.commentQuestionList[position])
                }
            } else {
                if (item.commentQuestionList.checkNotNull() && position < item.commentQuestionList.size && item.commentQuestionListTemp.contains(item.commentQuestionList[position])) {
                    item.commentQuestionListTemp.remove(item.commentQuestionList[position])
                }
            }
            true
        }
        var evaluationRatingBar: EvaluationRatingBar = helper.getView(R.id.erb_publish)
        evaluationRatingBar.setChangeRantingListener { rate ->
            item.commentResult = rate
            helper.setGone(R.id.tf_value, rate != 5)
        }
    }

}

